package com.alibaba.otter.manager.web.resource;

import com.alibaba.otter.manager.biz.dto.CommonSearchDTO;
import com.alibaba.otter.manager.biz.config.datamedia.DataMediaService;
import com.alibaba.otter.manager.biz.utils.DataSourceChecker;
import com.alibaba.otter.manager.web.common.model.SeniorDataMedia;
import com.alibaba.otter.manager.web.dto.RestResponse;
import com.alibaba.otter.manager.web.service.DataMediaAggService;
import com.alibaba.otter.manager.web.utils.PaginationUtil;
import com.alibaba.otter.shared.common.model.config.data.DataMedia;
import com.alibaba.otter.shared.common.model.config.data.db.DbDataMedia;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据表的配置信息
 */
@RequestMapping("/api/v1/data-media")
@RestController
public class DataMediaResource {

    private final DataMediaService dataMediaService;


    private final DataSourceChecker dataSourceChecker;


    private final DataMediaAggService dataMediaAggService;

    public DataMediaResource(DataMediaService dataMediaService, DataSourceChecker dataSourceChecker, DataMediaAggService dataMediaAggService) {
        this.dataMediaService = dataMediaService;
        this.dataSourceChecker = dataSourceChecker;
        this.dataMediaAggService = dataMediaAggService;
    }


    /**
     * 根据id查找树
     */
    @GetMapping("findOne/{id}")
    public RestResponse<DataMedia<?>> findOne(@PathVariable("id") Long id){
        return RestResponse.ok(dataMediaService.findById(id));
    }


    /**
     * 根据条件查询列表数据
     */
    @GetMapping("list")
    public RestResponse<List<SeniorDataMedia>> list(CommonSearchDTO searchVo, Pageable pageable){
        return PaginationUtil.pageHttpResponse(dataMediaAggService.page(searchVo, pageable));
    }


    /**
     * 添加
     */
    @PostMapping("add")
    public RestResponse<Void> create(@RequestBody @Validated DbDataMedia dbMediaSource){
        dataMediaService.create(dbMediaSource);
        return RestResponse.ok();
    }


    /**
     * 编辑
     */
    @PutMapping("edit")
    public RestResponse<Void> modify(@RequestBody @Validated DbDataMedia dbMediaSource){
        dataMediaService.modify(dbMediaSource);
        return RestResponse.ok();
    }


    /**
     * 删除
     */
    @DeleteMapping("delete/{id}")
    public RestResponse<Void> delete(@PathVariable("id") Long id){
        dataMediaService.remove(id);
        return RestResponse.ok();
    }

    /**
     * 检查链接
     */
    @PostMapping("checkConnection")
    public RestResponse<String> checkConnection(@RequestBody @Validated DbDataMedia dbMediaSource){
        String result = dataSourceChecker.checkMap(dbMediaSource.getNamespace(),
                dbMediaSource.getName(),
                dbMediaSource.getSource().getId());
        return RestResponse.ok(result);
    }

    /**
     * 获取表的命名空间+表明
     */
    @PostMapping("checkSchemaAndTable")
    public RestResponse<String> checkSchemaAndTable(@RequestBody @Validated DbDataMedia dbMediaSource){
        String result = dataSourceChecker.checkNamespaceTables(dbMediaSource.getNamespace(),
                dbMediaSource.getName(),
                dbMediaSource.getSource().getId());
        return RestResponse.ok(result);
    }

    /**
     * 获取表的命名空间+表明
     */
    @PostMapping("match-schema-table")
    public RestResponse<List<String>> matchSchemaAndTable(@RequestBody @Validated DbDataMedia dbMediaSource){
        List<String> result = dataSourceChecker.matchNamespaceTables(dbMediaSource.getNamespace(),
                dbMediaSource.getName(),
                dbMediaSource.getSource().getId());
        return RestResponse.ok(result);
    }
}
